package com.chainyoung.trademarket.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.constant.CommonStringConstants;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.common.appcommon.entity.MarketDetail;
import com.chainyoung.component_tmmarket.ui.MarketKLineActivity;
import com.chainyoung.trademarket.R;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;


public class IndexMarketAdapter extends BaseQuickAdapter<MarketDetail, BaseViewHolder> {

    private ImageLoader mImageLoader;
    //private String currencyOut="BTC";//交易区
    private Context mContext;
    private int width;

    public IndexMarketAdapter(Context context, @Nullable List<MarketDetail> data) {
        super(R.layout.layout_index_market_item, data);
        mImageLoader = Utils.getAppComponent().imageLoader();
        this.mContext = context;
        this.width = ScreenUtils.getScreenWidth(mContext) - ConvertUtils.dp2px(mContext, 30);
    }


    @Override
    protected void convert(BaseViewHolder helper, final MarketDetail item) {
        helper.itemView.setLayoutParams(new RecyclerView.LayoutParams(width / 3, ViewGroup.LayoutParams.WRAP_CONTENT));
        if (item.getId() == 0) {
            helper.getView(R.id.rl_market).setVisibility(View.INVISIBLE);
        } else {
            helper.getView(R.id.rl_market).setVisibility(View.VISIBLE);
//        mImageLoader.loadImage(mContext,
//                GlideImageConfig
//                        .builder()
//                        .url(item.getCoin_img())
//                        .errorPic(R.mipmap.ic_default)
//                        .imagerView((ImageView) (helper.getView(R.id.iv_coin_logo)))
//                        .build());

            helper.setText(R.id.tv_coin_name, item.getCoin().toUpperCase() + "/" + item.getRegion().toUpperCase());
            BigDecimal bd = new BigDecimal(item.getLast());
            helper.setText(R.id.tv_coin_price, bd.compareTo(BigDecimal.ONE) >= 0 ? bd.setScale(2, RoundingMode.DOWN).stripTrailingZeros().toPlainString() :
                    bd.setScale(5, RoundingMode.DOWN).toPlainString());
            helper.setText(R.id.tv_coin_price_cny, "≈" + new BigDecimal(item.getCny()).setScale(2, RoundingMode.DOWN).stripTrailingZeros().toPlainString() + "CNY");
            boolean b = new BigDecimal(item.getChange()).compareTo(BigDecimal.ZERO) >= 0;
            helper.setTextColor(R.id.tv_change, b ? ContextCompat.getColor(mContext, R.color._09C173) : ContextCompat.getColor(mContext, R.color._F95A54));
            helper.setTextColor(R.id.tv_coin_price, b ? ContextCompat.getColor(mContext, R.color._09C173) : ContextCompat.getColor(mContext, R.color._F95A54));
            // helper.setBackgroundRes(R.id.tv_change, b ? R.drawable.tmtrade_shape_green_solid : R.drawable.tmtrade_shape_red_solid);
            helper.setText(R.id.tv_change, b ? "+" + item.getChange() + "%" : item.getChange() + "%");

            helper.getView(R.id.rl_market).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, MarketKLineActivity.class);
                    intent.putExtra(CommonStringConstants.CURRENCY_IN, item.getCoin());
                    intent.putExtra(CommonStringConstants.CURRENCY_OUT, item.getRegion());
                    mContext.startActivity(intent);
                }
            });
        }

    }


}




